package com.mj.mes.config;/**
 * 异常处理类
 *
 * @author cRyann
 * @create 2017-04-07 13:03
 **/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 异常处理类
 * @author cRyann
 * @create 2017-04-07 13:03
 **/
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = Exception.class)
    public String defaultErrorHandler(HttpServletResponse rep, HttpServletRequest req, Exception e, Model model) throws IOException {
        String exception = e.getClass().getSimpleName();
        logger.error(e.getMessage(),e);
        String msg = "其他异常";
        switch (exception){
            case "UnknownAccountException":
                msg = "账号不存在"; break;
            case "IncorrectCredentialsException":
                msg = "密码不正确"; break;
            case "kaptchaValidateFailed":
                msg = "验证码错误"; break;
            case "UnsupportedTokenException":
                msg = "身份认证异常"; break;
            case "LockedAccountException":
                msg = "帐号锁定"; break;
            case "DisabledAccountException":
                msg = "用户禁用"; break;
            case "ExcessiveAttemptsException":
                msg = "登录重试次数超限"; break;
            case "HostUnauthorizedException":
                msg = "没有访问权限"; break;
            case "UnauthorizedException":
                msg = "没有访问权限"; break;
            default:
                break;
        }
        model.addAttribute("msg", msg);
        rep.setContentType("text/html; charset=utf-8");
        rep.getWriter().write("<p id='msg' style='display: none'>" + msg + "</p>");
        return req.getServletPath();
    }
}
